<jsp:include page="modules/header.jsp">
    <jsp:param name="active" value="home"/>
    <jsp:param name="webTitle" value="Local Discount"/>
</jsp:include>

<%@page import="java.sql.*,java.util.*,java.text.SimpleDateFormat" %>

<script type="text/JavaScript">
    function loadXMLDoc(url)
    {
        if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
        else
        {// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        var _ID = document.getElementById("fileUpImage").value;
        //_image = "\\img\\" + _fileUp;
        xmlhttp.open("GET",url+"?id="+_ID,false);

        xmlhttp.send(null);
        document.getElementById('info').innerHTML=xmlhttp.responseText;
    }

    function goTo(url){
        window.location = url;
    }

</script>

<div id="page">

    <%
            hbPackage.DatabaseAccess _da = new hbPackage.DatabaseAccess();
            SimpleDateFormat sf = new SimpleDateFormat("MM/dd/yyyy");
            ArrayList arr = _da.getWareHouseList(2);
            ArrayList<hbPackage.discountClass> arrDiscount = _da.getListDiscount(2);

            for (int i = 0; i < arrDiscount.size(); i++) {
                if (arrDiscount.get(i).getStatus().equals("available")) {
                    int _availableItem_ID = arrDiscount.get(i).getItem_ID();
                    for (int j = 0; j < arr.size(); j++) {
                        hbPackage.wareHouseClass _stock = (hbPackage.wareHouseClass) arr.get(j);
                        if (_stock.getItem_ID() == _availableItem_ID) {
                            arr.remove(_stock);
                        }
                    }
                }
            }

    %>

    <form action="../manageLocalDiscountSvl" method="POST">

        Item Name:
        <select name="dpd_itemName" id="fileUpImage" onchange="loadXMLDoc('itemInfo.jsp')">
            <option value="0" >Choose one item</option>
            <% for (int i = 0; i < arr.size(); i++) {
                hbPackage.wareHouseClass _wareHouse = (hbPackage.wareHouseClass) arr.get(i);
                hbPackage.itemClass _item = _da.getItem(_wareHouse.getItem_ID());
            %>

            <option value="<%= _item.getId()%>" ><%= _item.getItemName()%></option>

            <% }%>
        </select>

        <div id="info" style="height:auto;width:auto;">
        </div>

        Discount to  <input type="text" name="txt_discount" value="" maxlength="2" size="2"/> %
        From Date <input type="text" name="cal_fromDate" />
        To Date <input type="text" name="cal_toDate" />
        <input type="hidden" value="2" name="hid_storeID" />
        <input type="submit" value="Set" name="btn_submit" />
    </form>
    <%
            //ArrayList orderDetailList = (ArrayList) session.getAttribute("orderDetailList");




            if (arrDiscount != null) {
    %>

    <table>
        <tr>
            <th> Item Name </th>
            <th> Price </th>
            <th> Discount Percent </th>
            <th> From Date </th>
            <th> To Date </th>
            <th> Status </th>
            <th> Remove </th>
        </tr>
        <% for (int i = 0; i < arrDiscount.size(); i++) {
                    hbPackage.discountClass _discount = arrDiscount.get(i);
                    hbPackage.itemClass _item = _da.getItem(_discount.getItem_ID());
                    java.util.Date _today = new java.util.Date();
                    String _status = _discount.getStatus();
                    if(_today.after(_discount.getToDate())){
                        _status = "EXPIRED";
                    }

        %>
        <tr>
            <td><%= _item.getItemName()%></td>
            <td><%= _item.getPrice()%></td>
            <td><%= _discount.getPercent()%> (%) </td>
            <td><%= sf.format(_discount.getFromDate())%></td>
            <td><%= sf.format(_discount.getToDate())%></td>
            <td> <%= _status %> </td>
            <td>
                <% if (_discount.getStatus().equalsIgnoreCase("available")) { //if available, print button
%>
                <form method="post" action="../manageLocalDiscountSvl">
                    <input type="hidden" name="hid_dicountID" value="<%= _discount.getDiscountID() %>" />
                    <input type="submit" value="Remove" name="btn_submit" />
                </form>
                <%     } else {//if unavailable, print status
         out.println(_discount.getStatus());
     }
                %>
            </td>
        </tr>

        <%
                }//close loop
%>
    </table>
    <%
            } //close table
%>


</div>